package com.ui.basic.security.domain;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.jsecurity.authc.Account;
import org.jsecurity.authz.Permission;
import org.jsecurity.subject.PrincipalCollection;
import org.jsecurity.subject.SimplePrincipalCollection;

import com.ui.basic.ibatis.model.system.TblSMenu;
import com.ui.basic.ibatis.model.system.TblSRole;
import com.ui.basic.ibatis.model.system.TblSUser;

public class UserSecurity extends TblSUser implements Account {
	private TblSRole tblSRole;
	private List<TblSMenu> menus;
	
	public TblSRole getTblSRole() {
		return tblSRole;
	}

	public void setTblSRole(TblSRole tblSRole) {
		this.tblSRole = tblSRole;
	}

	public List<TblSMenu> getMenus() {
		return menus;
	}

	public void setMenus(List<TblSMenu> menus) {
		this.menus = menus;
	}

	public Object getCredentials() {
		return getPassword();
	}

	public PrincipalCollection getPrincipals() {
		return new SimplePrincipalCollection(getId(), "DefaultRealm");
	}

	public Collection<Permission> getObjectPermissions() {
		// NO-OP
		return null;
	}

	public Collection<String> getRoles() {
		List<String> rolenames = new ArrayList<String>();
		rolenames.add(getTblSRole().getRoleName());
		return rolenames;

	}

	public Collection<String> getStringPermissions() {
		List<String> stringPermissions = new ArrayList<String>();
		List<TblSMenu> menus = getMenus();
		for (TblSMenu menu : menus) {
			stringPermissions.add(menu.getMenuCode());
		}
		return stringPermissions;

	}

}
